#include<bits/stdc++.h>
using namespace std;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
    int f=0,x=0;
    char ch=getchar();
    while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return f?-x:x;
}
const int P=1e9+7;
int n;
int ls[2000000],rs[2000000],s[2000000],z[2000000],f[2000000];
#define ZIG 0
#define ZAG 1
void dfs(int x,int last,int zig,int zag,int zig0,int zag0,int zig1,int zag1)
{
    if(ls[x])
    {
        dfs(ls[x],ZIG,zig+1,zag,zig0+1,zag0+((zig+zag)%2&&last==ZIG),zig1+1,zag1+((zig+zag)%2==0&&last==ZIG));
    }
    if(rs[x])
    {
        dfs(rs[x],ZAG,zig,zag+1,zig0+((zig+zag)%2&&last==ZAG),zag0+1,zig1+((zig+zag)%2==0&&last==ZAG),zag1+1);
    }
    z[x]=z[ls[rs[x]]]+z[ls[ls[x]]]+z[rs[ls[x]]]+z[rs[rs[x]]]+1;
    f[ls[x]]=(f[ls[x]]+zig0)%P;
    f[rs[x]]=(f[rs[x]]+zag0)%P;
    f[ls[x]]=(f[ls[x]]+(zig1-1ll)*z[rs[x]]+(zig0-1ll)*(z[rs[ls[x]]]+z[rs[rs[x]]])-z[rs[rs[x]]])%P;
    f[rs[x]]=(f[rs[x]]+(zag1-1ll)*z[ls[x]]+(zag0-1ll)*(z[ls[rs[x]]]+z[ls[ls[x]]])-z[ls[ls[x]]])%P;
}
void calc(int x,int last,int zig,int zag,int zig0,int zag0,int zig1,int zag1,int depth)
{
    if(ls[x])
    {
        f[ls[x]]=(f[ls[x]]+f[x])%P;
        calc(ls[x],ZIG,zig+1,zag,zig0+1,zag0+((zig+zag)%2&&last==ZIG),zig1+1,zag1+((zig+zag)%2==0&&last==ZIG),depth+1);
    }
    if(rs[x])
    {
        f[rs[x]]=(f[rs[x]]+f[x])%P;
        calc(rs[x],ZAG,zig,zag+1,zig0+((zig+zag)%2&&last==ZAG),zag0+1,zig1+((zig+zag)%2==0&&last==ZAG),zag1+1,depth+1);
    }
    f[x]=(f[x]+(zig0-1ll)*(z[rs[ls[x]]]+z[rs[rs[x]]])-z[rs[rs[x]]])%P;
    f[x]=(f[x]+(zag0-1ll)*(z[ls[rs[x]]]+z[ls[ls[x]]])-z[ls[ls[x]]])%P;
    f[x]=(f[x]+(zig1-1ll)*z[rs[x]])%P;
    f[x]=(f[x]+(zag1-1ll)*z[ls[x]])%P;
    f[x]=((n-1ll)*depth-f[x]+P)%P;
}
int main()
{
    freopen("dream.in","r",stdin);
    freopen("dream.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++)
    {
        ls[i]=read();
        rs[i]=read();
    }
    dfs(1,-1,0,0,0,0,0,0);
    calc(1,-1,0,0,0,0,0,0,0);
    for(int i=1;i<=n;i++)
    {
        printf("%d\n",f[i]);
    }
    return 0;
}